const fs = require('fs');
const faker = require('faker');

function generateAccounts() {
    const accounts = [];
    accounts.push({
        account_nickname: 'superadmin',
        account_password: 'superpassword',
        permission_level: 0,
        full_name: '超级管理员',
        gender: true,
        contact_info: '1234567890',
        remarks: '系统管理员'
    });

    for (let i = 1; i < 20; i++) {
        accounts.push({
            account_nickname: faker.internet.userName().slice(0, 20),
            account_password: faker.internet.password().slice(0, 20),
            permission_level: 1,
            full_name: faker.name.findName().slice(0, 20),
            gender: faker.datatype.boolean(),
            contact_info: faker.phone.phoneNumber().slice(0, 20),
            remarks: faker.lorem.sentence().slice(0, 50)
        });
    }

    fs.writeFileSync('./data/accounts.txt', JSON.stringify(accounts, null, 2));
}

function generateGoods() {
    const goods = [];
    for (let i = 0; i < 50; i++) {
        goods.push({
            goods_name: faker.commerce.productName().slice(0, 50),
            goods_type: faker.commerce.department().slice(0, 50),
            remarks: faker.lorem.sentence().slice(0, 50)
        });
    }

    fs.writeFileSync('./data/goods.txt', JSON.stringify(goods, null, 2));
}

function generatePurchases() {
    const purchases = [];
    for (let i = 0; i < 100; i++) {
        purchases.push({
            goods_id: faker.datatype.number({ min: 1, max: 50 }),
            purchase_price: faker.commerce.price(),
            purchase_quantity: faker.datatype.number({ min: 1, max: 100 }),
            purchase_time: faker.date.past(),
            remarks: faker.lorem.sentence().slice(0, 50)
        });
    }

    fs.writeFileSync('./data/purchases.txt', JSON.stringify(purchases, null, 2));
}

function generateSales() {
    const sales = [];
    for (let i = 0; i < 100; i++) {
        sales.push({
            goods_id: faker.datatype.number({ min: 1, max: 50 }),
            sales_price: faker.commerce.price(),
            sales_quantity: faker.datatype.number({ min: 1, max: 100 }),
            sales_time: faker.date.past(),
            remarks: faker.lorem.sentence().slice(0, 50)
        });
    }

    fs.writeFileSync('./data/sales.txt', JSON.stringify(sales, null, 2));
}

function generateStock() {
    const stock = [];
    for (let i = 0; i < 50; i++) {
        const stock_quantity = faker.datatype.number({ min: 0, max: 1000 });
        const warning_threshold = faker.datatype.number({ min: 10, max: 100 });
        let stock_status = '充足';
        if (stock_quantity === 0) {
            stock_status = '缺货';
        } else if (stock_quantity <= warning_threshold) {
            stock_status = '紧张';
        }

        stock.push({
            goods_id: i + 1,
            stock_quantity,
            warning_threshold,
            stock_status,
            remarks: faker.lorem.sentence().slice(0, 50)
        });
    }

    fs.writeFileSync('./data/stock.txt', JSON.stringify(stock, null, 2));
}

generateAccounts();
generateGoods();
generatePurchases();
generateSales();
generateStock();
